package com.godpaper.as3.ds.sort
{
	/**
	 * 
	 * @author YangBo
	 * 
	 */	
	public class BubbleSort extends SortBase implements IBubbleSort
	{
		
		[Bindable]private var _sentinel:Number;
		[Bindable]private var _beSorted:Array;
		[Bindable]private var _sortResult:Array;
		
		public function BubbleSort()
		{
			//TODO: implement function
			super();
		}
		
		/**
		 * 
		 * @param value set a sort sentinel-box
		 * 
		 */		
		override public function set sentinel(value:Number):void
		{
			_sentinel = value;
		}
		/**
		 * 
		 * @return a sort sentinel's value 
		 * 
		 */		
		override public function get sentinel():Number
		{
			return _sentinel;
		}
		/**
		 * 
		 * @param value set beSorted data
		 * 
		 */		
		override public function set beSorted(value:Array):void
		{
			_beSorted = value;
		}
		/**
		 * 
		 * @return beSorted data
		 * 
		 */		
		override public function get beSorted():Array
		{
			return _beSorted;
		}
		/**
		 * 
		 * @param value set sort result
		 * 
		 */		
		override public function set sortResult(value:Array):void
		{
			_sortResult = value;			
		}
		/**
		 * 
		 * @return sort result
		 * 
		 */		
		override public function get sortResult():Array
		{
			return _sortResult;
		}
		/**
		 * 
		 * @param data indicates beSorted data
		 * @return sort result as array
		 * 
		 */	
		protected var count:int; 	
		override public function sort(data:Array):Array
		{
			//TODO: implement function
			beSorted = data;
			count = data.length;
			
			for(var i:int=1;i<count;i++)
			{
				for(var j:int=count-1;j>=i;j--)
				{
					if(data[j]<data[j-1])
					{
						data[j] = data[j]^data[j-1];
						data[j-1] = data[j]^data[j-1];
						data[j] = data[j]^data[j-1];
					}
				}
			}			
			sortResult = data;
			return sortResult;
		}
		/**
		 * 
		 * @param func dump the sort trace;
		 * 
		 */		
		override public function dump(func:Function):void
		{
			for(var i:int=0;i<count;i++)
			{
				trace(sortResult[i]+" ");
			}
			trace("\n");
		}
		
	}
}